<?php
/*Billing actions.
 *
 * @package    utility
 * @subpackage Billing
 * @author     Hammad Ali Naqvi
 * @version    SVN: $Id: actions.class.php 12479 2008-10-31 10:54:40Z fabien $ */
class BillingActions extends sfActions
{
  public function executeIndex(sfWebRequest $request)
  {
    //$this->forward('default', 'module');
  } // NOTHING IN IT
  
  public function executeListOfficerBill(sfWebRequest $request)
  {
    if($request->isMethod('POST'))
	{
		$billing_month = $request->getParameter('billing_month');
		$billing_year = $request->getParameter('billing_year');
		$station_id = $request->getParameter('station_id');
		$billing_date = $billing_year.'-'.$billing_month.'-'.'01';
			
		$d = new Criteria();
		$d->addAscendingOrderByColumn(BillingPeer::CONSUMER_NO);
		$d->add(BillingPeer::BILL_TYPE, 'officer');
		$d->add(BillingPeer::BILLING_DATE, $billing_date);
		$d->add(BillingPeer::STATION_ID, $station_id);
		$d->add(BillingPeer::STATUS, Constant::RECORD_STATUS_ACTIVE);
		$this->bills = BillingPeer::doSelectJoinAll($d);
		
		$display = BillingPeer::doSelectOne($d);
		
		$this->flag = true;
		if($display)
		{
			$this->station = $display->getStation();
			$this->bill_month = $display->getBillingDate('M-Y');
		}
	}
	
	else
	{
		$this->flag = false;
	}
  } // END - executeListOfficerBill
 
  public function executeListSoldierBill(sfWebRequest $request)
  {
    if($request->isMethod('POST'))
	{
		$billing_month = $request->getParameter('billing_month');
		$billing_year = $request->getParameter('billing_year');
		$station_id = $request->getParameter('station_id');
		$billing_date = $billing_year.'-'.$billing_month.'-'.'01';
			
		$d = new Criteria();
		$d->add(BillingPeer::BILL_TYPE, 'soldier');
		$d->add(BillingPeer::BILLING_DATE, $billing_date);
		$d->add(BillingPeer::STATION_ID, $station_id);
		$d->add(BillingPeer::STATUS, Constant::RECORD_STATUS_ACTIVE);
		$d->addAscendingOrderByColumn(BillingPeer::CONSUMER_NO);
		$this->bills = BillingPeer::doSelectJoinAll($d);
		
		$display = BillingPeer::doSelectOne($d);
		
		$this->flag = true;
		if($display)
		{
			$this->station = $display->getStation();
			$this->bill_month = $display->getBillingDate('M-Y');
		}
	}
	
	else
	{
		$this->flag = false;
	}
  } // END - executeListSoldierBill
  
  public function executeListCivilBill(sfWebRequest $request)
  {
    if($request->isMethod('POST'))
	{
		$billing_month = $request->getParameter('billing_month');
		$billing_year = $request->getParameter('billing_year');
		$station_id = $request->getParameter('station_id');
		$billing_date = $billing_year.'-'.$billing_month.'-'.'01';
			
		$d = new Criteria();
		$d->add(BillingPeer::BILL_TYPE, 'civil');
		$d->add(BillingPeer::BILLING_DATE, $billing_date);
		$d->add(BillingPeer::STATION_ID, $station_id);
		$d->add(BillingPeer::STATUS, Constant::RECORD_STATUS_ACTIVE);
		$d->addAscendingOrderByColumn(BillingPeer::CONSUMER_NO);
		$this->bills = BillingPeer::doSelectJoinAll($d);
		
		$display = BillingPeer::doSelectOne($d);
		
		$this->flag = true;
		if($display)
		{
			$this->station = $display->getStation();
			$this->bill_month = $display->getBillingDate('M-Y');
		}
	}
	
	else
	{
		$this->flag = false;
	}
  } // END - executeListCivilBill
  
  public function executeListCommercialBill(sfWebRequest $request)
  {
    if($request->isMethod('POST'))
	{
		$billing_month = $request->getParameter('billing_month');
		$billing_year = $request->getParameter('billing_year');
		$station_id = $request->getParameter('station_id');
		$billing_date = $billing_year.'-'.$billing_month.'-'.'01';
			
		$d = new Criteria();
		$d->add(BillingPeer::BILL_TYPE, 'commercial');
		$d->add(BillingPeer::BILLING_DATE, $billing_date);
		$d->add(BillingPeer::STATION_ID, $station_id);
		$d->add(BillingPeer::STATUS, Constant::RECORD_STATUS_ACTIVE);
		$d->addAscendingOrderByColumn(BillingPeer::CONSUMER_NO);
		$this->bills = BillingPeer::doSelectJoinAll($d);
		
		$display = BillingPeer::doSelectOne($d);
		
		$this->flag = true;
		if($display)
		{
			$this->station = $display->getStation();
			$this->bill_month = $display->getBillingDate('M-Y');
		}
	}
	
	else
	{
		$this->flag = false;
	}
  } // END - executeListCommercialBill
  
  public function executeAddOfficerBill(sfWebRequest $request)
  {
	if($request->isMethod('Post'))
		{
			$building_id = $request->getParameter('building_id');
			$this->building = BuildingPeer::retrieveByPk($request->getParameter('building_id'));		
	
			$c = new Criteria();
			$c->add(OccupantPeer::STATUS, Constant::RECORD_STATUS_ACTIVE);
			$c->add(OccupantPeer::OCCUPIED_STATUS, Constant::BUILDING_OCCUPIED);
			$c->add(OccupantPeer::BUILDING_ID, $building_id);
			$this->occupant = OccupantPeer::doSelectOne($c);
			
			$d = new Criteria();
			$d->add(BillingPeer::STATUS, Constant::RECORD_STATUS_ACTIVE);
			$d->add(BillingPeer::BUILDING_ID, $building_id);
			$d->addDescendingOrderByColumn(BillingPeer::ID);
			$this->previous_bill = BillingPeer::doSelectOne($d);
			
			$this->flag = true; 
		}// end IF
	
	else
		{
			$this->Station = array();
			$this->Building = array();

			foreach (StationPeer::GetStation() as $p)
			{
				$this->Station[$p->getId()] = $p->getTitle(); 
			}
		
			foreach (BuildingPeer::GetBuilding() as $s)
			{
				$this->Building[$s->getId()] = array($s->getStationId(), $s->getBuildingNo()); 
			}
			
			$this->flag = false;
		}// end else
  } // END - executeAddOfficerBill

  public function executeMakeOfficerBill(sfWebRequest $request)
  {
	  if($request->isMethod('Post'))
		{
			$billing_month = $request->getParameter('billing_month');
			$billing_year = $request->getParameter('billing_year');
			
			/*Preparing the Values*/
			$occupant_data = OccupantPeer::retrieveByPk($request->getParameter('occupant_id'));
			$station_id = $occupant_data->getBuilding()->getStationId();
			$building_id = $occupant_data->getBuildingId();
			$occupant_id = $occupant_data->getId();
			$consumer_no = $occupant_data->getBuilding()->getConsumerNo();
			$meter_no = $occupant_data->getBuilding()->getMeterNo();
			$meter_phase = $occupant_data->getBuilding()->getMeterPhase();
			$billing_date = '01-'.$billing_month.'-'.$billing_year; //$billing_date = $request->getParameter('billing_date');
			$previous =  $request->getParameter('previous_reading');
			$present =  $request->getParameter('present_reading');
			$furniture_charges =  $request->getParameter('furniture_charges');
			$split =  $request->getParameter('split_value');
			$window =  $request->getParameter('window_value');
			
			/*Different Chareges and Rents*/
			$calc_values = CalcValuePeer::doSelect(new Criteria());
			foreach($calc_values as $x=>$calc_value)
			{
				$value[$calc_value->getTitle()] = $calc_value->getValue();
			}
			
			$water_charges = $value['water_charges'];
			$tv_charges = $value['tv_charges'];
			$split_ac_units = $value['split_ac_units'];
			$window_ac_units = $value['window_ac_units'];
			// $eq_charges = $value['eq_charges'] / 100; To convert the value in percentage
			
			if ($meter_phase == Constant::SINGLE_PHASE_METER)
			{
				$meter_rent = $value['single_phase_rent'];
			}
			
			if ($meter_phase == Constant::THREE_PHASE_METER)
			{
				$meter_rent = $value['three_phase_rent'];
			}
			
			/* Get first slab value*/
			$c = new Criteria();
			$c->addAscendingOrderByColumn(ElecTarrifPeer::ID);
			$first_slab_data = ElecTarrifPeer::doSelectOne ($c);
			$first_slab = $first_slab_data->getToUnit();
			
			/* Get last slab value*/
			$c = new Criteria();
			$c->addDescendingOrderByColumn(ElecTarrifPeer::ID);
			$last_slab_data = ElecTarrifPeer::doSelectOne ($c);
			$last_slab = $last_slab_data->getFromUnit();
			
			$consumed_units = $present - $previous;
			$total_units = $consumed_units;
			
			$c = new Criteria();
			/*$c->add(ElecTarrifPeer::STATUS, Constant::RECORD_STATUS_DELETED, Criteria::NOT_EQUAL);*/
			$c->addAscendingOrderByColumn(ElecTarrifPeer::ID);
			$tarrifs = ElecTarrifPeer::doSelect ($c);
			
			$slab =  array();
			
			/*Make BILLING SLABS and get slab rates and units*/
			foreach($tarrifs as $i=>$tarrif)
			{
				if ($tarrif->getFromUnit() == $last_slab)
				{
					$slabs[$i] = $tarrif->getFromUnit() - 1 ;
				}
				else
				{
					$slabs[$i] = ( $tarrif->getToUnit() - $tarrif->getFromUnit() ) + 1 ;
				}
				$from[$i] = $tarrif->getFromUnit();
				$to[$i] = $tarrif->getToUnit();
				$officer_rate[$i] = $tarrif->getOfficerRate();
				// get rebate rate here if required
				$civil_rate[$i] = $tarrif->getCivilRate(); //for split and window ac calculation
			}
			
			/*BILLING OF ELECTRICITY ON OFFICER RATE*/
			$bill = 0;
			$split_ac_bill = 0;
			$window_ac_bill = 0;
			$total_split_ac_bill = 0;
			$total_window_ac_bill = 0;
			
			/*GAS AND MISC BILLING VALUES*/
			$gas_charges = $request->getParameter('gas_charges');
			$misc_charges = $request->getParameter('misc_charges');
			
			/*BILL OF UNITS 50 AND BELOW*/
			if ($total_units <= $first_slab)
			{
				$billed_unit = $total_units;
				$bill = $billed_unit * $officer_rate[0];
			}
			
			/*BILL OF ABOVE 50 UNITS*/
			else
			{
				$slabs[0] = NULL;
				foreach($slabs as $j=>$slab)
				{
					if($total_units != 0)
					{
						if($from[$j] == $last_slab)
						{
							$billed_unit[$j] = $total_units;	
							$total_units = $total_units - $total_units;
						}
						
						else if ($total_units < $slab)
						{
							$billed_unit[$j] = $total_units;
							$total_units = $total_units - $total_units;
						}
						else 
						{
							$billed_unit[$j] = $slab;
							$total_units = $total_units - $slab;
						}
						$bill = $bill + ($billed_unit[$j]*$officer_rate[$j]);
						//echo 'Slab = '.$slab.'<br />Units = '.$billed_unit[$j].'<br />Remaining = '.$total_units.'<br />BILL = '.$bill.'<br /><br />'; 
					}
				}
				/*END - BILLING OF ELECTRICITY ON OFFICER RATE*/
				
				/*BILLING OF SPLIT AC CHARGES*/
				if($split)
				{
					foreach($slabs as $k=>$slab)
					{
						if($split_ac_units != 0)
						{
							if($from[$k] == $last_slab)
							{
								$split_ac_billed_unit[$k] = $split_ac_units;	
								$split_ac_units = $split_ac_units - $split_ac_units;
							}
							
							else if ($split_ac_units < $slab)
							{
								$split_ac_billed_unit[$k] = $split_ac_units;
								$split_ac_units = $split_ac_units - $split_ac_units;
							}
							else 
							{
								$split_ac_billed_unit[$k] = $slab;
								$split_ac_units = $split_ac_units - $slab;
							}
							$split_ac_bill = $split_ac_bill + ($split_ac_billed_unit[$k]*$civil_rate[$k]);
						}
					}
					$total_split_ac_bill = $split_ac_bill*$split;
				}
				
				if($window)
				{
					foreach($slabs as $l=>$slab)
					{
						if($window_ac_units != 0)
						{
							if($from[$l] == $last_slab)
							{
								$window_ac_billed_unit[$l] = $window_ac_units;	
								$window_ac_units = $window_ac_units - $window_ac_units;
							}
							
							else if ($window_ac_units < $slab)
							{
								$window_ac_billed_unit[$l] = $window_ac_units;
								$window_ac_units = $window_ac_units - $window_ac_units;
							}
							else 
							{
								$window_ac_billed_unit[$l] = $slab;
								$window_ac_units = $window_ac_units - $slab;
							}
							$window_ac_bill = $window_ac_bill + ($window_ac_billed_unit[$l]*$civil_rate[$l]);
						}
					}
					$total_window_ac_bill = $window_ac_bill*$window;
				}
				
			}
			
			/*ROUNDING OFF THE BILL TO ZERO DECIMAL PLACES*/
			$bill = round ($bill,Constant::ROUND_RATING);
			$total_split_ac_bill = round ($total_split_ac_bill,Constant::ROUND_RATING);
			$total_window_ac_bill = round ($total_window_ac_bill,Constant::ROUND_RATING);
			
			if ($meter_phase == Constant::SINGLE_PHASE_METER && $bill < $value['single_phase_min'])
			{
				$bill = $value['single_phase_min'];
			}
			
			if ($meter_phase == Constant::THREE_PHASE_METER && $bill < $value['three_phase_min'])
			{
				$bill = $value['three_phase_min'];
			}
			
			//ENERGY QUOTIENT (EQ) Charges for Jhelum
			
			/*if ($consumed_units >= 300)
			{
					echo 'Consumed = '.$consumed_units;
					echo ' | | Bill= '.$bill;
					echo ' +++ EQ= '.$eq_rate = $bill * $eq_charges ;
					echo ' --- Bill+EQ= '.$bill = $bill + $eq_rate;
			}
			echo 'nothing --'.$bill ;
			exit();
			//END - ENERGY QUOTIENT (EQ)
			
			echo 'BILL = '. $bill;
			echo ' WATER = '.$water_charges;
			echo ' TV= '.$tv_charges ;
			echo ' RENT = '.$meter_rent ;
			echo ' FUR = '.$furniture ;
			echo ' SPLIT = '.$total_split_ac_bill;
			echo ' WINDOW = '.$total_window_ac_bill;
			echo ' Grand Total = '.*/
			$grand_total = $bill + $water_charges + $tv_charges + $meter_rent + $furniture_charges + $total_split_ac_bill + $total_window_ac_bill + $gas_charges + $misc_charges;
			//exit();
			$billing = new Billing();
			
			$billing->setStationId($station_id);
			$billing->setBuildingId($building_id);
			$billing->setOccupantId($occupant_id);
			$billing->setMeterNo($meter_no);
			$billing->setConsumerNo($consumer_no);
			$billing->setBillingDate($billing_date);
			$billing->setPreviousReading($previous);
			$billing->setPresentReading($present);
			$billing->setUnitsConsumed($consumed_units);
			$billing->setWaterCharges($water_charges);
			$billing->setTvCharges($tv_charges);
			$billing->setGasCharges($gas_charges);
			$billing->setMiscCharges($misc_charges);
			$billing->setMeterRent($meter_rent);
			$billing->setUnitsTotal($bill);
			$billing->setFurnitureCharges($furniture_charges);
			$billing->setSplitCharges($total_split_ac_bill);
			$billing->setWindowCharges($total_window_ac_bill);
			$billing->setGrandTotal($grand_total);
			$billing->setPrintStatus(Constant::BOOLEAN_WORD_NO);
			$billing->setPaymentStatus(Constant::BOOLEAN_WORD_NO);
			$billing->setBillType('officer');
			$billing->setBillingCategory('On Meter');
			$billing->setStatus(Constant::RECORD_STATUS_ACTIVE);
			
			$billing->save();
			$this->getUser()->setFlash('SUCCESS_MESSAGE', Constant::RECORD_ADDED_SUCCESSFULLY);
			$this->redirect ('Billing/index');
		}
  }// END - executeMakeOfficerBill
  
  public function executeAddSoldierBill(sfWebRequest $request)
  {
	if($request->isMethod('Post'))
		{
			$building_id = $request->getParameter('building_id');
			$this->building = BuildingPeer::retrieveByPk($request->getParameter('building_id'));		
	
			$c = new Criteria();
			$c->add(OccupantPeer::STATUS, Constant::RECORD_STATUS_ACTIVE);
			$c->add(OccupantPeer::OCCUPIED_STATUS, Constant::BUILDING_OCCUPIED);
			$c->add(OccupantPeer::BUILDING_ID, $building_id);
			$this->occupant = OccupantPeer::doSelectOne($c);
			
			$d = new Criteria();
			$d->add(BillingPeer::STATUS, Constant::RECORD_STATUS_ACTIVE);
			$d->add(BillingPeer::BUILDING_ID, $building_id);
			$d->addDescendingOrderByColumn(BillingPeer::ID);
			$this->previous_bill = BillingPeer::doSelectOne($d);
			
			//print_r($previous_bill); exit();
			$this->flag = true; 
		}// end IF
	
	else
		{
			$this->Station = array();
			$this->Building = array();

			foreach (StationPeer::GetStation() as $p)
			{
				$this->Station[$p->getId()] = $p->getTitle(); 
			}
		
			foreach (BuildingPeer::GetBuilding() as $s)
			{
				$this->Building[$s->getId()] = array($s->getStationId(), $s->getBuildingNo());
			}

			$this->flag = false;
		}// end else
  } // END - executeAddSoldierBill
  
  public function executeMakeSoldierBill(sfWebRequest $request)
  {
	  if($request->isMethod('Post'))
		{
			$billing_month = $request->getParameter('billing_month');
			$billing_year = $request->getParameter('billing_year');
			
			/*Preparing the Values*/
			$occupant_data = OccupantPeer::retrieveByPk($request->getParameter('occupant_id'));
			$station_id = $occupant_data->getBuilding()->getStationId();
			$building_id = $occupant_data->getBuildingId();
			$occupant_id = $occupant_data->getId();
			$consumer_no = $occupant_data->getBuilding()->getConsumerNo();
			$meter_no = $occupant_data->getBuilding()->getMeterNo();
			$meter_phase = $occupant_data->getBuilding()->getMeterPhase();
			$billing_date = '01-'.$billing_month.'-'.$billing_year;//$request->getParameter('billing_date');
			$previous =  $request->getParameter('previous_reading');
			$present =  $request->getParameter('present_reading');
			$furniture_charges =  $request->getParameter('furniture_charges');
			$split =  $request->getParameter('split_value');
			$window =  $request->getParameter('window_value');
			$free_unit_type = $request->getParameter('free[0]');
			
			/*Different Chareges and Rents*/
			$calc_values = CalcValuePeer::doSelect(new Criteria());
			foreach($calc_values as $x=>$calc_value)
			{
				$value[$calc_value->getTitle()] = $calc_value->getValue();
			}
			
			$water_charges = 0; // JCO and Soldiers don't pay water charges
			$tv_charges = $value['tv_charges'];
			$split_ac_units = $value['split_ac_units'];
			$window_ac_units = $value['window_ac_units'];
			
			if ($meter_phase == Constant::SINGLE_PHASE_METER)
			{
				$meter_rent = $value['single_phase_rent'];
			}
			
			if ($meter_phase == Constant::THREE_PHASE_METER)
			{
				$meter_rent = $value['three_phase_rent'];
			}
			
			if ($free_unit_type == Constant::SOLDIER_FREE_UNITS_WINTER)
			{
				$free_unit = $value['soldier_winter'];
			}
			
			if ($free_unit_type == Constant::SOLDIER_FREE_UNITS_SUMMER)
			{
				$free_unit = $value['soldier_summer'];
			}
			
			/* Get first slab value*/
			$c = new Criteria();
			$c->addAscendingOrderByColumn(ElecTarrifPeer::ID);
			$first_slab_data = ElecTarrifPeer::doSelectOne ($c);
			$first_slab = $first_slab_data->getToUnit();
			
			/* Get last slab value*/
			$c = new Criteria();
			$c->addDescendingOrderByColumn(ElecTarrifPeer::ID);
			$last_slab_data = ElecTarrifPeer::doSelectOne ($c);
			$last_slab = $last_slab_data->getFromUnit();
			
			$consumed_units = $present - $previous;
			$total_units = $consumed_units - $free_unit;
			
			$c = new Criteria();
			/*$c->add(ElecTarrifPeer::STATUS, Constant::RECORD_STATUS_DELETED, Criteria::NOT_EQUAL);*/
			$c->addAscendingOrderByColumn(ElecTarrifPeer::ID);
			$tarrifs = ElecTarrifPeer::doSelect ($c);
			
			$slab =  array();
			
			/*Make BILLING SLABS and get slab rates and units*/
			foreach($tarrifs as $i=>$tarrif)
			{
				if ($tarrif->getFromUnit() == $last_slab)
				{
					$slabs[$i] = $tarrif->getFromUnit() - 1 ;
				}
				else
				{
					$slabs[$i] = ( $tarrif->getToUnit() - $tarrif->getFromUnit() ) + 1 ;
				}
				$from[$i] = $tarrif->getFromUnit();
				$to[$i] = $tarrif->getToUnit();
				// get rebate rate here if required
				$civil_rate[$i] = $tarrif->getCivilRate();
			}
			
			/*BILLING OF ELECTRICITY ON JCO/OR Rate*/
			$bill = 0;
			$split_ac_bill = 0;
			$window_ac_bill = 0;
			$total_split_ac_bill = 0;
			$total_window_ac_bill = 0;
			
			/*GAS AND MISC BILLING VALUES*/
			$gas_charges = $request->getParameter('gas_charges');
			$misc_charges = $request->getParameter('misc_charges');
			
			/*BILL OF UNITS 50 AND BELOW*/
			if ($total_units <= $first_slab)
			{
				$billed_unit = $total_units;
				
				$bill = $billed_unit * $civil_rate[0];
			}
			
			/*BILL OF ABOVE 50 UNITS*/
			else
			{
				$slabs[0] = NULL;
				foreach($slabs as $j=>$slab)
				{
					if($total_units != 0)
					{
						if($from[$j] == $last_slab)
						{
							$billed_unit[$j] = $total_units;	
							$total_units = $total_units - $total_units;
						}
						
						else if ($total_units < $slab)
						{
							$billed_unit[$j] = $total_units;
							$total_units = $total_units - $total_units;
						}
						else 
						{
							$billed_unit[$j] = $slab;
							$total_units = $total_units - $slab;
						}
						$bill = $bill + ($billed_unit[$j]*$civil_rate[$j]);
						//echo 'Slab = '.$slab.'<br />Units = '.$billed_unit[$j].'<br />Remaining = '.$total_units.'<br />BILL = '.$bill.'<br /><br />'; 
					}
				}
				/*END - BILLING OF ELECTRICITY ON OFFICER RATE*/
				
				/*BILLING OF SPLIT AC CHARGES*/
				if($split)
				{
					foreach($slabs as $k=>$slab)
					{
						if($split_ac_units != 0)
						{
							if($from[$k] == $last_slab)
							{
								$split_ac_billed_unit[$k] = $split_ac_units;	
								$split_ac_units = $split_ac_units - $split_ac_units;
							}
							
							else if ($split_ac_units < $slab)
							{
								$split_ac_billed_unit[$k] = $split_ac_units;
								$split_ac_units = $split_ac_units - $split_ac_units;
							}
							else 
							{
								$split_ac_billed_unit[$k] = $slab;
								$split_ac_units = $split_ac_units - $slab;
							}
							$split_ac_bill = $split_ac_bill + ($split_ac_billed_unit[$k]*$civil_rate[$k]);
						}
					}
					$total_split_ac_bill = $split_ac_bill*$split;
				}
				
				if($window)
				{
					foreach($slabs as $l=>$slab)
					{
						if($window_ac_units != 0)
						{
							if($from[$l] == $last_slab)
							{
								$window_ac_billed_unit[$l] = $window_ac_units;	
								$window_ac_units = $window_ac_units - $window_ac_units;
							}
							
							else if ($window_ac_units < $slab)
							{
								$window_ac_billed_unit[$l] = $window_ac_units;
								$window_ac_units = $window_ac_units - $window_ac_units;
							}
							else 
							{
								$window_ac_billed_unit[$l] = $slab;
								$window_ac_units = $window_ac_units - $slab;
							}
							$window_ac_bill = $window_ac_bill + ($window_ac_billed_unit[$l]*$civil_rate[$l]);
						}
					}
					$total_window_ac_bill = $window_ac_bill*$window;
				}
				
			}
			
			/*ROUNDING OFF THE BILL TO ZERO DECIMAL PLACES*/
			$bill = round ($bill,Constant::ROUND_RATING);
			$total_split_ac_bill = round ($total_split_ac_bill,Constant::ROUND_RATING);
			$total_window_ac_bill = round ($total_window_ac_bill,Constant::ROUND_RATING);
			
			if ($meter_phase == Constant::SINGLE_PHASE_METER && $bill < $value['single_phase_min'])
			{
				$bill = $value['single_phase_min'];
			}
			
			if ($meter_phase == Constant::THREE_PHASE_METER && $bill < $value['three_phase_min'])
			{
				$bill = $value['three_phase_min'];
			}
			
			// Soldiers don't give Water Charges so it is not included
			$grand_total = $bill + $tv_charges + $meter_rent + $furniture_charges + $total_split_ac_bill + $total_window_ac_bill + $gas_charges +$misc_charges; 
			
			$billing = new Billing();
			
			$billing->setStationId($station_id);
			$billing->setBuildingId($building_id);
			$billing->setOccupantId($occupant_id);
			$billing->setMeterNo($meter_no);
			$billing->setConsumerNo($consumer_no);
			$billing->setBillingDate($billing_date);
			$billing->setPreviousReading($previous);
			$billing->setPresentReading($present);
			$billing->setUnitsConsumed($consumed_units);
			$billing->setWaterCharges($water_charges);
			$billing->setTvCharges($tv_charges);
			$billing->setGasCharges($gas_charges);
			$billing->setMiscCharges($misc_charges);
			$billing->setMeterRent($meter_rent);
			$billing->setUnitsTotal($bill);
			$billing->setUnitsFree($free_unit);
			$billing->setFurnitureCharges($furniture_charges);
			$billing->setSplitCharges($total_split_ac_bill);
			$billing->setWindowCharges($total_window_ac_bill);
			$billing->setGrandTotal($grand_total);
			$billing->setPrintStatus(Constant::BOOLEAN_WORD_NO);
			$billing->setPaymentStatus(Constant::BOOLEAN_WORD_NO);
			$billing->setBillType('soldier');
			$billing->setBillingCategory('On Meter');
			$billing->setStatus(Constant::RECORD_STATUS_ACTIVE);
			
			$billing->save();
			$this->getUser()->setFlash('SUCCESS_MESSAGE', Constant::RECORD_ADDED_SUCCESSFULLY);
			$this->redirect ('Billing/index');
		}
  }// END -  executeMakeSoldierBill
  
  public function executeAddJcoBill(sfWebRequest $request)
  {
	if($request->isMethod('Post'))
		{
			$building_id = $request->getParameter('building_id');
			$this->building = BuildingPeer::retrieveByPk($request->getParameter('building_id'));		
	
			$c = new Criteria();
			$c->add(OccupantPeer::STATUS, Constant::RECORD_STATUS_ACTIVE);
			$c->add(OccupantPeer::OCCUPIED_STATUS, Constant::BUILDING_OCCUPIED);
			$c->add(OccupantPeer::BUILDING_ID, $building_id);
			$this->occupant = OccupantPeer::doSelectOne($c);
			
			$d = new Criteria();
			$d->add(BillingPeer::STATUS, Constant::RECORD_STATUS_ACTIVE);
			$d->add(BillingPeer::BUILDING_ID, $building_id);
			$d->addDescendingOrderByColumn(BillingPeer::ID);
			$this->previous_bill = BillingPeer::doSelectOne($d);
			
			$this->flag = true; 
		}// end IF
	
	else
		{
			$this->Station = array();
			$this->Building = array();

			foreach (StationPeer::GetStation() as $p)
			{
				$this->Station[$p->getId()] = $p->getTitle(); 
			}
		
			foreach (BuildingPeer::GetBuilding() as $s)
			{
				$this->Building[$s->getId()] = array($s->getStationId(), $s->getBuildingNo());
			}

			$this->flag = false;
		}// end else
  } // END - executeAddJcoBill
  
  public function executeMakeJcoBill(sfWebRequest $request)
  {
	  if($request->isMethod('Post'))
		{
			$billing_month = $request->getParameter('billing_month');
			$billing_year = $request->getParameter('billing_year');
			
			/*Preparing the Values*/
			$occupant_data = OccupantPeer::retrieveByPk($request->getParameter('occupant_id'));
			$station_id = $occupant_data->getBuilding()->getStationId();
			$building_id = $occupant_data->getBuildingId();
			$occupant_id = $occupant_data->getId();
			$consumer_no = $occupant_data->getBuilding()->getConsumerNo();
			$meter_no = $occupant_data->getBuilding()->getMeterNo();
			$meter_phase = $occupant_data->getBuilding()->getMeterPhase();
			$billing_date = '01-'.$billing_month.'-'.$billing_year;//$request->getParameter('billing_date');
			$previous =  $request->getParameter('previous_reading');
			$present =  $request->getParameter('present_reading');
			$furniture_charges =  $request->getParameter('furniture_charges');
			$split =  $request->getParameter('split_value');
			$window =  $request->getParameter('window_value');
			$free_unit_type = $request->getParameter('free[0]');
			
			/*Different Chareges and Rents*/
			$calc_values = CalcValuePeer::doSelect(new Criteria());
			foreach($calc_values as $x=>$calc_value)
			{
				$value[$calc_value->getTitle()] = $calc_value->getValue();
			}
			
			$water_charges = 0; // JCO and Soldiers don't pay water charges
			$tv_charges = $value['tv_charges'];
			$split_ac_units = $value['split_ac_units'];
			$window_ac_units = $value['window_ac_units'];
			
			if ($meter_phase == Constant::SINGLE_PHASE_METER)
			{
				$meter_rent = $value['single_phase_rent'];
			}
			
			if ($meter_phase == Constant::THREE_PHASE_METER)
			{
				$meter_rent = $value['three_phase_rent'];
			}
			
			if ($free_unit_type == Constant::SOLDIER_FREE_UNITS_WINTER)
			{
				$free_unit = $value['jco_winter'];
			}
			
			if ($free_unit_type == Constant::SOLDIER_FREE_UNITS_SUMMER)
			{
				$free_unit = $value['jco_summer'];
			}
			
			/* Get first slab value*/
			$c = new Criteria();
			$c->addAscendingOrderByColumn(ElecTarrifPeer::ID);
			$first_slab_data = ElecTarrifPeer::doSelectOne ($c);
			$first_slab = $first_slab_data->getToUnit();
			
			/* Get last slab value*/
			$c = new Criteria();
			$c->addDescendingOrderByColumn(ElecTarrifPeer::ID);
			$last_slab_data = ElecTarrifPeer::doSelectOne ($c);
			$last_slab = $last_slab_data->getFromUnit();
			
			$consumed_units = $present - $previous;
			$total_units = $consumed_units - $free_unit;
			
			$c = new Criteria();
			/*$c->add(ElecTarrifPeer::STATUS, Constant::RECORD_STATUS_DELETED, Criteria::NOT_EQUAL);*/
			$c->addAscendingOrderByColumn(ElecTarrifPeer::ID);
			$tarrifs = ElecTarrifPeer::doSelect ($c);
			
			$slab =  array();
			
			/*Make BILLING SLABS and get slab rates and units*/
			foreach($tarrifs as $i=>$tarrif)
			{
				if ($tarrif->getFromUnit() == $last_slab)
				{
					$slabs[$i] = $tarrif->getFromUnit() - 1 ;
				}
				else
				{
					$slabs[$i] = ( $tarrif->getToUnit() - $tarrif->getFromUnit() ) + 1 ;
				}
				$from[$i] = $tarrif->getFromUnit();
				$to[$i] = $tarrif->getToUnit();
				// get rebate rate here if required
				$civil_rate[$i] = $tarrif->getCivilRate();
			}
			
			/*BILLING OF ELECTRICITY ON JCO RATE*/
			$bill = 0;
			$split_ac_bill = 0;
			$window_ac_bill = 0;
			$total_split_ac_bill = 0;
			$total_window_ac_bill = 0;
			
			/*GAS AND MISC BILLING VALUES*/
			$gas_charges = $request->getParameter('gas_charges');
			$misc_charges = $request->getParameter('misc_charges');
			
			/*BILL OF UNITS 50 AND BELOW*/
			if ($total_units <= $first_slab)
			{
				$billed_unit = $total_units;
				
				$bill = $billed_unit * $civil_rate[0];
			}
			
			/*BILL OF ABOVE 50 UNITS*/
			else
			{
				$slabs[0] = NULL;
				foreach($slabs as $j=>$slab)
				{
					if($total_units != 0)
					{
						if($from[$j] == $last_slab)
						{
							$billed_unit[$j] = $total_units;	
							$total_units = $total_units - $total_units;
						}
						
						else if ($total_units < $slab)
						{
							$billed_unit[$j] = $total_units;
							$total_units = $total_units - $total_units;
						}
						else 
						{
							$billed_unit[$j] = $slab;
							$total_units = $total_units - $slab;
						}
						$bill = $bill + ($billed_unit[$j]*$civil_rate[$j]);
					}
				}
				/*END - BILLING OF ELECTRICITY ON JCO RATE*/
				
				/*BILLING OF SPLIT AC CHARGES*/
				if($split)
				{
					foreach($slabs as $k=>$slab)
					{
						if($split_ac_units != 0)
						{
							if($from[$k] == $last_slab)
							{
								$split_ac_billed_unit[$k] = $split_ac_units;	
								$split_ac_units = $split_ac_units - $split_ac_units;
							}
							
							else if ($split_ac_units < $slab)
							{
								$split_ac_billed_unit[$k] = $split_ac_units;
								$split_ac_units = $split_ac_units - $split_ac_units;
							}
							else 
							{
								$split_ac_billed_unit[$k] = $slab;
								$split_ac_units = $split_ac_units - $slab;
							}
							$split_ac_bill = $split_ac_bill + ($split_ac_billed_unit[$k]*$civil_rate[$k]);
						}
					}
					$total_split_ac_bill = $split_ac_bill*$split;
				}
				
				if($window)
				{
					foreach($slabs as $l=>$slab)
					{
						if($window_ac_units != 0)
						{
							if($from[$l] == $last_slab)
							{
								$window_ac_billed_unit[$l] = $window_ac_units;	
								$window_ac_units = $window_ac_units - $window_ac_units;
							}
							
							else if ($window_ac_units < $slab)
							{
								$window_ac_billed_unit[$l] = $window_ac_units;
								$window_ac_units = $window_ac_units - $window_ac_units;
							}
							else 
							{
								$window_ac_billed_unit[$l] = $slab;
								$window_ac_units = $window_ac_units - $slab;
							}
							$window_ac_bill = $window_ac_bill + ($window_ac_billed_unit[$l]*$civil_rate[$l]);
						}
					}
					$total_window_ac_bill = $window_ac_bill*$window;
				}
				
			}
			
			/*ROUNDING OFF THE BILL TO ZERO DECIMAL PLACES*/
			$bill = round ($bill,Constant::ROUND_RATING);
			$total_split_ac_bill = round ($total_split_ac_bill,Constant::ROUND_RATING);
			$total_window_ac_bill = round ($total_window_ac_bill,Constant::ROUND_RATING);
			
			if ($meter_phase == Constant::SINGLE_PHASE_METER && $bill < $value['single_phase_min'])
			{
				$bill = $value['single_phase_min'];
			}
			
			if ($meter_phase == Constant::THREE_PHASE_METER && $bill < $value['three_phase_min'])
			{
				$bill = $value['three_phase_min'];
			}
			
			// Water Charges not included in JCO's Bill
			$grand_total = $bill + $tv_charges + $meter_rent + $furniture_charges + $total_split_ac_bill + $total_window_ac_bill +$gas_charges +$misc_charges;
			
			$billing = new Billing();
			
			$billing->setStationId($station_id);
			$billing->setBuildingId($building_id);
			$billing->setOccupantId($occupant_id);
			$billing->setMeterNo($meter_no);
			$billing->setConsumerNo($consumer_no);
			$billing->setBillingDate($billing_date);
			$billing->setPreviousReading($previous);
			$billing->setPresentReading($present);
			$billing->setUnitsConsumed($consumed_units);
			$billing->setWaterCharges($water_charges);
			$billing->setTvCharges($tv_charges);
			$billing->setGasCharges($gas_charges);
			$billing->setMiscCharges($misc_charges);
			$billing->setMeterRent($meter_rent);
			$billing->setUnitsTotal($bill);
			$billing->setUnitsFree($free_unit);
			$billing->setFurnitureCharges($furniture_charges);
			$billing->setSplitCharges($total_split_ac_bill);
			$billing->setWindowCharges($total_window_ac_bill);
			$billing->setGrandTotal($grand_total);
			$billing->setPrintStatus(Constant::BOOLEAN_WORD_NO);
			$billing->setPaymentStatus(Constant::BOOLEAN_WORD_NO);
			$billing->setBillType('soldier');
			$billing->setBillingCategory('On Meter');
			$billing->setStatus(Constant::RECORD_STATUS_ACTIVE);
			
			$billing->save();
			$this->getUser()->setFlash('SUCCESS_MESSAGE', Constant::RECORD_ADDED_SUCCESSFULLY);
			$this->redirect ('Billing/index');
		}
  }// END -  executeMakeJcoBill
  
  public function executeAddCivilBill(sfWebRequest $request)
  {
	if($request->isMethod('Post'))
		{
			$building_id = $request->getParameter('building_id');
			$this->building = BuildingPeer::retrieveByPk($request->getParameter('building_id'));		
	
			$c = new Criteria();
			$c->add(OccupantPeer::STATUS, Constant::RECORD_STATUS_ACTIVE);
			$c->add(OccupantPeer::OCCUPIED_STATUS, Constant::BUILDING_OCCUPIED);
			$c->add(OccupantPeer::BUILDING_ID, $building_id);
			$this->occupant = OccupantPeer::doSelectOne($c);
			
			$d = new Criteria();
			$d->add(BillingPeer::STATUS, Constant::RECORD_STATUS_ACTIVE);
			$d->add(BillingPeer::BUILDING_ID, $building_id);
			$d->addDescendingOrderByColumn(BillingPeer::ID);
			$this->previous_bill = BillingPeer::doSelectOne($d);
			
			$this->flag = true; 
		}// end IF
	
	else
		{
			$this->Station = array();
			$this->Building = array();

			foreach (StationPeer::GetStation() as $p)
			{
				$this->Station[$p->getId()] = $p->getTitle(); 
			}
		
			foreach (BuildingPeer::GetBuilding() as $s)
			{
				$this->Building[$s->getId()] = array($s->getStationId(), $s->getBuildingNo()); 
			}

			$this->flag = false;
		}// end else
  } // END - executeAddCivilBill
  
  public function executeMakeCivilBill(sfWebRequest $request)
  {
	  if($request->isMethod('Post'))
		{
			$billing_month = $request->getParameter('billing_month');
			$billing_year = $request->getParameter('billing_year');
			
			/*Preparing the Values*/
			$occupant_data = OccupantPeer::retrieveByPk($request->getParameter('occupant_id'));
			$station_id = $occupant_data->getBuilding()->getStationId();
			$building_id = $occupant_data->getBuildingId();
			$occupant_id = $occupant_data->getId();
			$consumer_no = $occupant_data->getBuilding()->getConsumerNo();
			$meter_no = $occupant_data->getBuilding()->getMeterNo();
			$meter_phase = $occupant_data->getBuilding()->getMeterPhase();
			$billing_date = '01-'.$billing_month.'-'.$billing_year;//$request->getParameter('billing_date');
			$previous =  $request->getParameter('previous_reading');
			$present =  $request->getParameter('present_reading');
			$furniture_charges =  $request->getParameter('furniture_charges');
			$split =  $request->getParameter('split_value');
			$window =  $request->getParameter('window_value');
			
			/*Different Chareges and Rents*/
			$calc_values = CalcValuePeer::doSelect(new Criteria());
			foreach($calc_values as $x=>$calc_value)
			{
				$value[$calc_value->getTitle()] = $calc_value->getValue();
			}
			
			$water_charges = $value['water_charges'];
			$tv_charges = $value['tv_charges'];
			$split_ac_units = $value['split_ac_units'];
			$window_ac_units = $value['window_ac_units'];
			
			if ($meter_phase == Constant::SINGLE_PHASE_METER)
			{
				$meter_rent = $value['single_phase_rent'];
			}
			
			if ($meter_phase == Constant::THREE_PHASE_METER)
			{
				$meter_rent = $value['three_phase_rent'];
			}
			
			/* Get first slab value*/
			$c = new Criteria();
			$c->addAscendingOrderByColumn(ElecTarrifPeer::ID);
			$first_slab_data = ElecTarrifPeer::doSelectOne ($c);
			$first_slab = $first_slab_data->getToUnit();
			
			/* Get last slab value*/
			$c = new Criteria();
			$c->addDescendingOrderByColumn(ElecTarrifPeer::ID);
			$last_slab_data = ElecTarrifPeer::doSelectOne ($c);
			$last_slab = $last_slab_data->getFromUnit();
			
			$consumed_units = $present - $previous;
			$total_units = $consumed_units;
			
			$c = new Criteria();
			/*$c->add(ElecTarrifPeer::STATUS, Constant::RECORD_STATUS_DELETED, Criteria::NOT_EQUAL);*/
			$c->addAscendingOrderByColumn(ElecTarrifPeer::ID);
			$tarrifs = ElecTarrifPeer::doSelect ($c);
			
			$slab =  array();
			
			/*Make BILLING SLABS and get slab rates and units*/
			foreach($tarrifs as $i=>$tarrif)
			{
				if ($tarrif->getFromUnit() == $last_slab)
				{
					$slabs[$i] = $tarrif->getFromUnit() - 1 ;
				}
				else
				{
					$slabs[$i] = ( $tarrif->getToUnit() - $tarrif->getFromUnit() ) + 1 ;
				}
				$from[$i] = $tarrif->getFromUnit();
				$to[$i] = $tarrif->getToUnit();
				// get rebate rate here if required
				$civil_rate[$i] = $tarrif->getCivilRate();
			}
			
			/*BILLING OF ELECTRICITY ON JCO RATE*/
			$bill = 0;
			$split_ac_bill = 0;
			$window_ac_bill = 0;
			$total_split_ac_bill = 0;
			$total_window_ac_bill = 0;
			
			/*GAS AND MISC BILLING VALUES*/
			$gas_charges = $request->getParameter('gas_charges');
			$misc_charges = $request->getParameter('misc_charges');
			
			/*BILL OF UNITS 50 AND BELOW*/
			if ($total_units <= $first_slab)
			{
				$billed_unit = $total_units;
				
				$bill = $billed_unit * $civil_rate[0];
			}
			
			/*BILL OF ABOVE 50 UNITS*/
			else
			{
				$slabs[0] = NULL;
				foreach($slabs as $j=>$slab)
				{
					if($total_units != 0)
					{
						if($from[$j] == $last_slab)
						{
							$billed_unit[$j] = $total_units;	
							$total_units = $total_units - $total_units;
						}
						
						else if ($total_units < $slab)
						{
							$billed_unit[$j] = $total_units;
							$total_units = $total_units - $total_units;
						}
						else 
						{
							$billed_unit[$j] = $slab;
							$total_units = $total_units - $slab;
						}
						$bill = $bill + ($billed_unit[$j]*$civil_rate[$j]);
					}
				}
				/*END - BILLING OF ELECTRICITY ON JCO RATE*/
				
				/*BILLING OF SPLIT AC CHARGES*/
				if($split)
				{
					foreach($slabs as $k=>$slab)
					{
						if($split_ac_units != 0)
						{
							if($from[$k] == $last_slab)
							{
								$split_ac_billed_unit[$k] = $split_ac_units;	
								$split_ac_units = $split_ac_units - $split_ac_units;
							}
							
							else if ($split_ac_units < $slab)
							{
								$split_ac_billed_unit[$k] = $split_ac_units;
								$split_ac_units = $split_ac_units - $split_ac_units;
							}
							else 
							{
								$split_ac_billed_unit[$k] = $slab;
								$split_ac_units = $split_ac_units - $slab;
							}
							$split_ac_bill = $split_ac_bill + ($split_ac_billed_unit[$k]*$civil_rate[$k]);
						}
					}
					$total_split_ac_bill = $split_ac_bill*$split;
				}
				
				if($window)
				{
					foreach($slabs as $l=>$slab)
					{
						if($window_ac_units != 0)
						{
							if($from[$l] == $last_slab)
							{
								$window_ac_billed_unit[$l] = $window_ac_units;	
								$window_ac_units = $window_ac_units - $window_ac_units;
							}
							
							else if ($window_ac_units < $slab)
							{
								$window_ac_billed_unit[$l] = $window_ac_units;
								$window_ac_units = $window_ac_units - $window_ac_units;
							}
							else 
							{
								$window_ac_billed_unit[$l] = $slab;
								$window_ac_units = $window_ac_units - $slab;
							}
							$window_ac_bill = $window_ac_bill + ($window_ac_billed_unit[$l]*$civil_rate[$l]);
						}
					}
					$total_window_ac_bill = $window_ac_bill*$window;
				}
				
			}
			
			/*ROUNDING OFF THE BILL TO ZERO DECIMAL PLACES*/
			$bill = round ($bill,Constant::ROUND_RATING);
			$total_split_ac_bill = round ($total_split_ac_bill,Constant::ROUND_RATING);
			$total_window_ac_bill = round ($total_window_ac_bill,Constant::ROUND_RATING);
			
			if ($meter_phase == Constant::SINGLE_PHASE_METER && $bill < $value['single_phase_min'])
			{
				$bill = $value['single_phase_min'];
			}
			
			if ($meter_phase == Constant::THREE_PHASE_METER && $bill < $value['three_phase_min'])
			{
				$bill = $value['three_phase_min'];
			}
			
			$grand_total = $bill + $water_charges + $tv_charges + $meter_rent + $furniture_charges + $total_split_ac_bill + $total_window_ac_bill +$gas_charges +$misc_charges;
			
			$billing = new Billing();
			
			$billing->setStationId($station_id);
			$billing->setBuildingId($building_id);
			$billing->setOccupantId($occupant_id);
			$billing->setMeterNo($meter_no);
			$billing->setConsumerNo($consumer_no);
			$billing->setBillingDate($billing_date);
			$billing->setPreviousReading($previous);
			$billing->setPresentReading($present);
			$billing->setUnitsConsumed($consumed_units);
			$billing->setWaterCharges($water_charges);
			$billing->setTvCharges($tv_charges);
			$billing->setGasCharges($gas_charges);
			$billing->setMiscCharges($misc_charges);
			$billing->setMeterRent($meter_rent);
			$billing->setUnitsTotal($bill);
			$billing->setUnitsFree($free_unit);
			$billing->setFurnitureCharges($furniture_charges);
			$billing->setSplitCharges($total_split_ac_bill);
			$billing->setWindowCharges($total_window_ac_bill);
			$billing->setGrandTotal($grand_total);
			$billing->setPrintStatus(Constant::BOOLEAN_WORD_NO);
			$billing->setPaymentStatus(Constant::BOOLEAN_WORD_NO);
			$billing->setBillType('civil');
			$billing->setBillingCategory('On Meter');
			$billing->setStatus(Constant::RECORD_STATUS_ACTIVE);
			
			$billing->save();
			$this->getUser()->setFlash('SUCCESS_MESSAGE', Constant::RECORD_ADDED_SUCCESSFULLY);
			$this->redirect ('Billing/index');
		}
  }// END -  executeMakeCivilBill
  
  public function executeAddCommercialBill(sfWebRequest $request)
  {
	if($request->isMethod('Post'))
		{
			$building_id = $request->getParameter('building_id');
			$this->building = BuildingPeer::retrieveByPk($request->getParameter('building_id'));		
	
			$c = new Criteria();
			$c->add(OccupantPeer::STATUS, Constant::RECORD_STATUS_ACTIVE);
			$c->add(OccupantPeer::OCCUPIED_STATUS, Constant::BUILDING_OCCUPIED);
			$c->add(OccupantPeer::BUILDING_ID, $building_id);
			$this->occupant = OccupantPeer::doSelectOne($c);
			
			$d = new Criteria();
			$d->add(BillingPeer::STATUS, Constant::RECORD_STATUS_ACTIVE);
			$d->add(BillingPeer::BUILDING_ID, $building_id);
			$d->addDescendingOrderByColumn(BillingPeer::ID);
			$this->previous_bill = BillingPeer::doSelectOne($d);
			
			$this->flag = true; 
		}// end IF
	
	else
		{
			$this->Station = array();
			$this->Building = array();

			foreach (StationPeer::GetStation() as $p)
			{
				$this->Station[$p->getId()] = $p->getTitle(); 
			}
		
			foreach (BuildingPeer::GetBuilding() as $s)
			{
				$this->Building[$s->getId()] = array($s->getStationId(), $s->getBuildingNo()); 
			}

			$this->flag = false;
		}// end else
  } // END - executeAddCommercialBill
  
  public function executeMakeCommercialBill(sfWebRequest $request)
  {
	  if($request->isMethod('Post'))
		{
			$billing_month = $request->getParameter('billing_month');
			$billing_year = $request->getParameter('billing_year');
			
			/*Preparing the Values*/
			$occupant_data = OccupantPeer::retrieveByPk($request->getParameter('occupant_id'));
			$station_id = $occupant_data->getBuilding()->getStationId();
			$building_id = $occupant_data->getBuildingId();
			$occupant_id = $occupant_data->getId();
			$consumer_no = $occupant_data->getBuilding()->getConsumerNo();
			$meter_no = $occupant_data->getBuilding()->getMeterNo();
			$meter_phase = $occupant_data->getBuilding()->getMeterPhase();
			$billing_date = '01-'.$billing_month.'-'.$billing_year;//$request->getParameter('billing_date');
			$previous =  $request->getParameter('previous_reading');
			$present =  $request->getParameter('present_reading');
			$furniture_charges =  $request->getParameter('furniture_charges');
			$split =  $request->getParameter('split_value');
			$window =  $request->getParameter('window_value');
			
			/*Different Chareges and Rents*/
			$calc_values = CalcValuePeer::doSelect(new Criteria());
			foreach($calc_values as $x=>$calc_value)
			{
				$value[$calc_value->getTitle()] = $calc_value->getValue();
			}
			
			$water_charges = $value['water_charges'];
			$tv_charges = $value['tv_charges'];
			$split_ac_units = $value['split_ac_units'];
			$window_ac_units = $value['window_ac_units'];
			$commercial_rate = $value['commercial_rate'];
			
			if ($meter_phase == Constant::SINGLE_PHASE_METER)
			{
				$meter_rent = $value['single_phase_rent'];
			}
			
			if ($meter_phase == Constant::THREE_PHASE_METER)
			{
				$meter_rent = $value['three_phase_rent'];
			}
			
			
			/* Get first slab value*/
			$c = new Criteria();
			$c->addAscendingOrderByColumn(ElecTarrifPeer::ID);
			$first_slab_data = ElecTarrifPeer::doSelectOne ($c);
			$first_slab = $first_slab_data->getToUnit();
			
			/* Get last slab value*/
			$c = new Criteria();
			$c->addDescendingOrderByColumn(ElecTarrifPeer::ID);
			$last_slab_data = ElecTarrifPeer::doSelectOne ($c);
			$last_slab = $last_slab_data->getFromUnit();
			
			$consumed_units = $present - $previous;
			$total_units = $consumed_units;
			
			/*BILLING OF ELECTRICITY ON COMMERCIAL RATE*/
			$billed_units = $total_units;
			$bill = $billed_units*$commercial_rate;
			/*END - BILLING OF ELECTRICITY ON COMMERCIAL RATE*/
			
			
			$c = new Criteria();
			/*$c->add(ElecTarrifPeer::STATUS, Constant::RECORD_STATUS_DELETED, Criteria::NOT_EQUAL);*/
			$c->addAscendingOrderByColumn(ElecTarrifPeer::ID);
			$tarrifs = ElecTarrifPeer::doSelect ($c);
			
			$slab =  array();
			
			/*Make BILLING SLABS and get slab rates and units*/
			foreach($tarrifs as $i=>$tarrif)
			{
				if ($tarrif->getFromUnit() == $last_slab)
				{
					$slabs[$i] = $tarrif->getFromUnit() - 1 ;
				}
				else
				{
					$slabs[$i] = ( $tarrif->getToUnit() - $tarrif->getFromUnit() ) + 1 ;
				}
				$from[$i] = $tarrif->getFromUnit();
				$to[$i] = $tarrif->getToUnit();
				// get rebate rate here if required
				$civil_rate[$i] = $tarrif->getCivilRate();
			}
			
			$split_ac_bill = 0;
			$window_ac_bill = 0;
			$total_split_ac_bill = 0;
			$total_window_ac_bill = 0;
			
			/*GAS AND MISC BILLING VALUES*/
			$gas_charges = $request->getParameter('gas_charges');
			$misc_charges = $request->getParameter('misc_charges');
				
				/*BILLING OF SPLIT AC CHARGES*/
				if($split)
				{
					foreach($slabs as $k=>$slab)
					{
						if($split_ac_units != 0)
						{
							if($from[$k] == $last_slab)
							{
								$split_ac_billed_unit[$k] = $split_ac_units;	
								$split_ac_units = $split_ac_units - $split_ac_units;
							}
							
							else if ($split_ac_units < $slab)
							{
								$split_ac_billed_unit[$k] = $split_ac_units;
								$split_ac_units = $split_ac_units - $split_ac_units;
							}
							else 
							{
								$split_ac_billed_unit[$k] = $slab;
								$split_ac_units = $split_ac_units - $slab;
							}
							$split_ac_bill = $split_ac_bill + ($split_ac_billed_unit[$k]*$civil_rate[$k]);
						}
					}
					$total_split_ac_bill = $split_ac_bill*$split;
				}
				
				if($window)
				{
					foreach($slabs as $l=>$slab)
					{
						if($window_ac_units != 0)
						{
							if($from[$l] == $last_slab)
							{
								$window_ac_billed_unit[$l] = $window_ac_units;	
								$window_ac_units = $window_ac_units - $window_ac_units;
							}
							
							else if ($window_ac_units < $slab)
							{
								$window_ac_billed_unit[$l] = $window_ac_units;
								$window_ac_units = $window_ac_units - $window_ac_units;
							}
							else 
							{
								$window_ac_billed_unit[$l] = $slab;
								$window_ac_units = $window_ac_units - $slab;
							}
							$window_ac_bill = $window_ac_bill + ($window_ac_billed_unit[$l]*$civil_rate[$l]);
						}
					}
					$total_window_ac_bill = $window_ac_bill*$window;
				}
				
			
			
			/*ROUNDING OFF THE BILL TO ZERO DECIMAL PLACES*/
			$bill = round ($bill,Constant::ROUND_RATING);
			$total_split_ac_bill = round ($total_split_ac_bill,Constant::ROUND_RATING);
			$total_window_ac_bill = round ($total_window_ac_bill,Constant::ROUND_RATING);
			
			if ($meter_phase == Constant::SINGLE_PHASE_METER && $bill < $value['single_phase_min'])
			{
				$bill = $value['single_phase_min'];
			}
			
			if ($meter_phase == Constant::THREE_PHASE_METER && $bill < $value['three_phase_min'])
			{
				$bill = $value['three_phase_min'];
			}
			
			$grand_total = $bill + $water_charges + $tv_charges + $meter_rent + $furniture_charges + $total_split_ac_bill + $total_window_ac_bill;
			
			$billing = new Billing();
			
			$billing->setStationId($station_id);
			$billing->setBuildingId($building_id);
			$billing->setOccupantId($occupant_id);
			$billing->setMeterNo($meter_no);
			$billing->setConsumerNo($consumer_no);
			$billing->setBillingDate($billing_date);
			$billing->setPreviousReading($previous);
			$billing->setPresentReading($present);
			$billing->setUnitsConsumed($consumed_units);
			$billing->setWaterCharges($water_charges);
			$billing->setTvCharges($tv_charges);
			$billing->setGasCharges($gas_charges);
			$billing->setMiscCharges($misc_charges);
			$billing->setMeterRent($meter_rent);
			$billing->setUnitsTotal($bill);
			$billing->setFurnitureCharges($furniture_charges);
			$billing->setSplitCharges($total_split_ac_bill);
			$billing->setWindowCharges($total_window_ac_bill);
			$billing->setGrandTotal($grand_total);
			$billing->setPrintStatus(Constant::BOOLEAN_WORD_NO);
			$billing->setPaymentStatus(Constant::BOOLEAN_WORD_NO);
			$billing->setBillType('commercial');
			$billing->setBillingCategory('On Meter');
			$billing->setStatus(Constant::RECORD_STATUS_ACTIVE);
			
			$billing->save();
			$this->getUser()->setFlash('SUCCESS_MESSAGE', Constant::RECORD_ADDED_SUCCESSFULLY);
			$this->redirect ('Billing/index');
		}
  }// END -  executeMakeCommercialBill
  
  public function executeDetailBill(sfWebRequest $request)
  {
		$bill_data = BillingPeer::retrieveByPk(Utility::DecryptQueryString($request->getParameter('id')));
		
		$this->bill = BillingPeer::retrieveByPk(Utility::DecryptQueryString($request->getParameter('id')));
		$this->bill_type = $bill_data->getBillType();
		$this->tarrifRates = ElecTarrifPeer::doSelect(new Criteria);
		
		// data for History of Building.
		$buliding_id = $bill_data->getBuildingId();
		$c = new Criteria();
		$c->add(BillingPeer::STATUS, Constant::RECORD_STATUS_ACTIVE);
		$c->add(BillingPeer::BUILDING_ID, $buliding_id);
		$this->previous_bills = BillingPeer::doSelect($c);
	
  }// END - executeDetailBill
  
   public function executePrintBill(sfWebRequest $request)
  {
		$this->setLayout(false);
		$bill_data = BillingPeer::retrieveByPk(Utility::DecryptQueryString($request->getParameter('id')));
		
		$this->bill = BillingPeer::retrieveByPk(Utility::DecryptQueryString($request->getParameter('id')));
		$this->bill_type = $bill_data->getBillType();
		$this->tarrifRates = ElecTarrifPeer::doSelect(new Criteria);
		
		// data for History of Building.
		$buliding_id = $bill_data->getBuildingId();
		$c = new Criteria();
		$c->add(BillingPeer::STATUS, Constant::RECORD_STATUS_ACTIVE);
		$c->add(BillingPeer::BUILDING_ID, $buliding_id);
		$this->previous_bills = BillingPeer::doSelect($c);
	
  }// END - executePrintBill
  
  public function executeAddFlatBill(sfWebRequest $request)
  {
	if($request->isMethod('Post'))
		{
			$building_id = $request->getParameter('building_id');
			$this->building = BuildingPeer::retrieveByPk($request->getParameter('building_id'));		
	
			$c = new Criteria();
			$c->add(OccupantPeer::STATUS, Constant::RECORD_STATUS_ACTIVE);
			$c->add(OccupantPeer::OCCUPIED_STATUS, Constant::BUILDING_OCCUPIED);
			$c->add(OccupantPeer::BUILDING_ID, $building_id);
			$this->occupant = OccupantPeer::doSelectOne($c);
			
			$d = new Criteria();
			$d->add(BillingPeer::BUILDING_ID, $building_id);
			$d->add(BillingPeer::STATUS, Constant::RECORD_STATUS_ACTIVE);
			$d->addDescendingOrderByColumn(BillingPeer::ID);
			$this->previous_bill = BillingPeer::doSelectOne($d);
			
			$this->flag = true; 
			$this->bill_type = $request->getParameter('bill_type');
		}// end IF
	
	else
		{
			$this->Station = array();
			$this->Building = array();

			foreach (StationPeer::GetStation() as $p)
			{
				$this->Station[$p->getId()] = $p->getTitle(); 
			}
		
			foreach (BuildingPeer::GetBuilding() as $s)
			{
				$this->Building[$s->getId()] = array($s->getStationId(), $s->getBuildingNo()); 
			}
			
			$this->flag = false;
			$this->bill_type = $request->getParameter('type');
		}// end else
  } // END - executeAddFlatBill

  public function executeMakeFlatBill(sfWebRequest $request)
  {
	  if($request->isMethod('Post'))
		{
			$billing_month = $request->getParameter('billing_month');
			$billing_year = $request->getParameter('billing_year');
			$bill_type = $request->getParameter('bill_type');
			
/*Preparing the Values*/
			$occupant_data = OccupantPeer::retrieveByPk($request->getParameter('occupant_id'));
			$station_id = $occupant_data->getBuilding()->getStationId();
			$building_id = $occupant_data->getBuildingId();
			$occupant_id = $occupant_data->getId();
			$consumer_no = $occupant_data->getBuilding()->getConsumerNo();
			$meter_no = $occupant_data->getBuilding()->getMeterNo();
			$meter_phase = $occupant_data->getBuilding()->getMeterPhase();
			$billing_date = '01-'.$billing_month.'-'.$billing_year;
			$previous =  $request->getParameter('previous_reading');
			$present =  $request->getParameter('previous_reading');
			$flat_bill_units = $request->getParameter('flat_bill_units');
			$furniture_charges =  $request->getParameter('furniture_charges');
			$split =  $request->getParameter('split_value');
			$window =  $request->getParameter('window_value');
			$free_unit_type = $request->getParameter('free[0]');
			
/*Different Chareges and Rents*/
			$calc_values = CalcValuePeer::doSelect(new Criteria());
			foreach($calc_values as $x=>$calc_value)
			{
				$value[$calc_value->getTitle()] = $calc_value->getValue();
			}
			
			$water_charges = $value['water_charges'];
			$tv_charges = $value['tv_charges'];
			$split_ac_units = $value['split_ac_units'];
			$window_ac_units = $value['window_ac_units'];
			$commercial_rate = $value['commercial_rate'];
			
			if ($meter_phase == Constant::SINGLE_PHASE_METER) $meter_rent = $value['single_phase_rent'];
			if ($meter_phase == Constant::THREE_PHASE_METER) $meter_rent = $value['three_phase_rent'];
			
			if ($free_unit_type == Constant::JCO_FREE_WINTER) $free_unit = $value['jco_winter'];
			else if ($free_unit_type == Constant::JCO_FREE_SUMMER) $free_unit = $value['jco_summer'];
			else if ($free_unit_type == Constant::SOLDIER_FREE_WINTER) $free_unit = $value['soldier_winter'];
			else if ($free_unit_type == Constant::SOLDIER_FREE_SUMMER) $free_unit = $value['soldier_summer'];
			else $free_unit = 0;
			
/* Get first slab value*/
			$c = new Criteria();
			$c->addAscendingOrderByColumn(ElecTarrifPeer::ID);
			$first_slab_data = ElecTarrifPeer::doSelectOne ($c);
			$first_slab = $first_slab_data->getToUnit();
			
/* Get last slab value*/
			$c = new Criteria();
			$c->addDescendingOrderByColumn(ElecTarrifPeer::ID);
			$last_slab_data = ElecTarrifPeer::doSelectOne ($c);
			$last_slab = $last_slab_data->getFromUnit();
			
			$consumed_units = $flat_bill_units;
			
			$c = new Criteria();
			/*$c->add(ElecTarrifPeer::STATUS, Constant::RECORD_STATUS_DELETED, Criteria::NOT_EQUAL);*/
			$c->addAscendingOrderByColumn(ElecTarrifPeer::ID);
			$tarrifs = ElecTarrifPeer::doSelect ($c);
			
			$slab =  array();
			
/*Make BILLING SLABS and get slab rates and units*/
			foreach($tarrifs as $i=>$tarrif)
			{
				if ($tarrif->getFromUnit() == $last_slab)
				{
					$slabs[$i] = $tarrif->getFromUnit() - 1 ;
				}
				else
				{
					$slabs[$i] = ( $tarrif->getToUnit() - $tarrif->getFromUnit() ) + 1 ;
				}
				$from[$i] = $tarrif->getFromUnit();
				$to[$i] = $tarrif->getToUnit();
				$officer_rate[$i] = $tarrif->getOfficerRate();
				$civil_rate[$i] = $tarrif->getCivilRate(); //for split and window ac calculation
			}
			
/*BILLING OF ELECTRICITY*/
			$total_units = $consumed_units;
			
			$bill = 0;
			$split_ac_bill = 0;
			$window_ac_bill = 0;
			$total_split_ac_bill = 0;
			$total_window_ac_bill = 0;
			
			/*GAS AND MISC BILLING VALUES*/
			$gas_charges = $request->getParameter('gas_charges');
			$misc_charges = $request->getParameter('misc_charges');
			
/*BILLING OF SPLIT AC CHARGES*/
				if($split)
				{
					foreach($slabs as $k=>$slab)
					{
						if($split_ac_units != 0)
						{
							if($from[$k] == $last_slab)
							{
								$split_ac_billed_unit[$k] = $split_ac_units;	
								$split_ac_units = $split_ac_units - $split_ac_units;
							}
							
							else if ($split_ac_units < $slab)
							{
								$split_ac_billed_unit[$k] = $split_ac_units;
								$split_ac_units = $split_ac_units - $split_ac_units;
							}
							else 
							{
								$split_ac_billed_unit[$k] = $slab;
								$split_ac_units = $split_ac_units - $slab;
							}
							$split_ac_bill = $split_ac_bill + ($split_ac_billed_unit[$k]*$civil_rate[$k]);
						}
					}
					$total_split_ac_bill = $split_ac_bill*$split;
				}
				
/*BILLING OF WINDOW AC CHARGES*/				
				if($window)
				{
					foreach($slabs as $l=>$slab)
					{
						if($window_ac_units != 0)
						{
							if($from[$l] == $last_slab)
							{
								$window_ac_billed_unit[$l] = $window_ac_units;	
								$window_ac_units = $window_ac_units - $window_ac_units;
							}
							
							else if ($window_ac_units < $slab)
							{
								$window_ac_billed_unit[$l] = $window_ac_units;
								$window_ac_units = $window_ac_units - $window_ac_units;
							}
							else 
							{
								$window_ac_billed_unit[$l] = $slab;
								$window_ac_units = $window_ac_units - $slab;
							}
							$window_ac_bill = $window_ac_bill + ($window_ac_billed_unit[$l]*$civil_rate[$l]);
						}
					}
					$total_window_ac_bill = $window_ac_bill*$window;
				}
				
			
if ($bill_type == 'officer')
{
	$total_units = $consumed_units;
/*BILL OF UNITS 50 AND BELOW*/
			if ($total_units <= $first_slab)
			{
				$billed_unit = $total_units;
				$bill = $billed_unit * $officer_rate[0];
			}
			
/*BILL OF ABOVE 50 UNITS*/
			else
			{
				$slabs[0] = NULL;
				foreach($slabs as $j=>$slab)
				{
					if($total_units != 0)
					{
						if($from[$j] == $last_slab)
						{
							$billed_unit[$j] = $total_units;	
							$total_units = $total_units - $total_units;
						}
						
						else if ($total_units < $slab)
						{
							$billed_unit[$j] = $total_units;
							$total_units = $total_units - $total_units;
						}
						else 
						{
							$billed_unit[$j] = $slab;
							$total_units = $total_units - $slab;
						}
						$bill = $bill + ($billed_unit[$j]*$officer_rate[$j]);
					}
				}
/*END - BILLING OF ELECTRICITY ON OFFICER RATE*/
			}
			
			/*ROUNDING OFF THE BILL TO ZERO DECIMAL PLACES*/
			$bill = round ($bill,Constant::ROUND_RATING);
			$total_split_ac_bill = round ($total_split_ac_bill,Constant::ROUND_RATING);
			$total_window_ac_bill = round ($total_window_ac_bill,Constant::ROUND_RATING);
			
			if ($meter_phase == Constant::SINGLE_PHASE_METER && $bill < $value['single_phase_min']) $bill = $value['single_phase_min'];
			if ($meter_phase == Constant::THREE_PHASE_METER && $bill < $value['three_phase_min']) $bill = $value['three_phase_min'];
			
			$grand_total = $bill + $water_charges + $tv_charges + $meter_rent + $furniture_charges + $total_split_ac_bill + $total_window_ac_bill + $gas_charges +$misc_charges;
			$bill_type_for_db = 'officer';				
} // END IF OF officer TYPE
			
if ($bill_type == 'jco')
{
$total_units = $consumed_units - $free_unit;
/*BILL OF UNITS 50 AND BELOW*/
			if ($total_units <= $first_slab)
			{
				$billed_unit = $total_units;
				$bill = $billed_unit * $civil_rate[0];
			}
			
/*BILL OF ABOVE 50 UNITS*/
			else
			{
				$slabs[0] = NULL;
				foreach($slabs as $j=>$slab)
				{
					if($total_units != 0)
					{
						if($from[$j] == $last_slab)
						{
							$billed_unit[$j] = $total_units;	
							$total_units = $total_units - $total_units;
						}
						
						else if ($total_units < $slab)
						{
							$billed_unit[$j] = $total_units;
							$total_units = $total_units - $total_units;
						}
						else 
						{
							$billed_unit[$j] = $slab;
							$total_units = $total_units - $slab;
						}
						$bill = $bill + ($billed_unit[$j]*$civil_rate[$j]);
					}
				}
/*END - BILLING OF ELECTRICITY ON OFFICER RATE*/
			}
			
			/*ROUNDING OFF THE BILL TO ZERO DECIMAL PLACES*/
			$bill = round ($bill,Constant::ROUND_RATING);
			$total_split_ac_bill = round ($total_split_ac_bill,Constant::ROUND_RATING);
			$total_window_ac_bill = round ($total_window_ac_bill,Constant::ROUND_RATING);
			
			if ($meter_phase == Constant::SINGLE_PHASE_METER && $bill < $value['single_phase_min']) $bill = $value['single_phase_min'];
			if ($meter_phase == Constant::THREE_PHASE_METER && $bill < $value['three_phase_min']) $bill = $value['three_phase_min'];
			
			$grand_total = $bill + $tv_charges + $meter_rent + $furniture_charges + $total_split_ac_bill + $total_window_ac_bill + $gas_charges +$misc_charges;
			$bill_type_for_db = 'soldier';
			$water_charges = 0; // JCO and Soldiers don't pay water charges					
} // END IF OF jco TYPE
			
if ($bill_type == 'soldier')
{
$total_units = $consumed_units - $free_unit;
/*BILL OF UNITS 50 AND BELOW*/
			if ($total_units <= $first_slab)
			{
				$billed_unit = $total_units;
				$bill = $billed_unit * $civil_rate[0];
			}
			
/*BILL OF ABOVE 50 UNITS*/
			else
			{
				$slabs[0] = NULL;
				foreach($slabs as $j=>$slab)
				{
					if($total_units != 0)
					{
						if($from[$j] == $last_slab)
						{
							$billed_unit[$j] = $total_units;	
							$total_units = $total_units - $total_units;
						}
						
						else if ($total_units < $slab)
						{
							$billed_unit[$j] = $total_units;
							$total_units = $total_units - $total_units;
						}
						else 
						{
							$billed_unit[$j] = $slab;
							$total_units = $total_units - $slab;
						}
						$bill = $bill + ($billed_unit[$j]*$civil_rate[$j]);
					}
				}
/*END - BILLING OF ELECTRICITY ON OFFICER RATE*/
			}
			
			/*ROUNDING OFF THE BILL TO ZERO DECIMAL PLACES*/
			$bill = round ($bill,Constant::ROUND_RATING);
			$total_split_ac_bill = round ($total_split_ac_bill,Constant::ROUND_RATING);
			$total_window_ac_bill = round ($total_window_ac_bill,Constant::ROUND_RATING);
			
			if ($meter_phase == Constant::SINGLE_PHASE_METER && $bill < $value['single_phase_min']) $bill = $value['single_phase_min'];
			if ($meter_phase == Constant::THREE_PHASE_METER && $bill < $value['three_phase_min']) $bill = $value['three_phase_min'];
			
			$grand_total = $bill + $tv_charges + $meter_rent + $furniture_charges + $total_split_ac_bill + $total_window_ac_bill + $gas_charges +$misc_charges;
			$bill_type_for_db = 'soldier';
			$water_charges = 0; // JCO and Soldiers don't pay water charges				
} // END IF OF soldier TYPE

if ($bill_type == 'civil')
{
$total_units = $consumed_units;
/*BILL OF UNITS 50 AND BELOW*/
			if ($total_units <= $first_slab)
			{
				$billed_unit = $total_units;
				$bill = $billed_unit * $civil_rate[0];
			}
			
/*BILL OF ABOVE 50 UNITS*/
			else
			{
				$slabs[0] = NULL;
				foreach($slabs as $j=>$slab)
				{
					if($total_units != 0)
					{
						if($from[$j] == $last_slab)
						{
							$billed_unit[$j] = $total_units;	
							$total_units = $total_units - $total_units;
						}
						
						else if ($total_units < $slab)
						{
							$billed_unit[$j] = $total_units;
							$total_units = $total_units - $total_units;
						}
						else 
						{
							$billed_unit[$j] = $slab;
							$total_units = $total_units - $slab;
						}
						$bill = $bill + ($billed_unit[$j]*$civil_rate[$j]);
					}
				}
/*END - BILLING OF ELECTRICITY ON OFFICER RATE*/
			}
			
			/*ROUNDING OFF THE BILL TO ZERO DECIMAL PLACES*/
			$bill = round ($bill,Constant::ROUND_RATING);
			$total_split_ac_bill = round ($total_split_ac_bill,Constant::ROUND_RATING);
			$total_window_ac_bill = round ($total_window_ac_bill,Constant::ROUND_RATING);
			
			if ($meter_phase == Constant::SINGLE_PHASE_METER && $bill < $value['single_phase_min']) $bill = $value['single_phase_min'];
			if ($meter_phase == Constant::THREE_PHASE_METER && $bill < $value['three_phase_min']) $bill = $value['three_phase_min'];
			
			$grand_total = $bill + $water_charges + $tv_charges + $meter_rent + $furniture_charges + $total_split_ac_bill + $total_window_ac_bill + $gas_charges +$misc_charges;
			$bill_type_for_db = 'civil';					
} // END IF OF civil TYPE

if ($bill_type == 'commercial')
{
			$total_units = $consumed_units;
			
			/*BILLING OF ELECTRICITY ON COMMERCIAL RATE*/
			$billed_units = $total_units;
			$bill = $billed_units*$commercial_rate;
			
			/*ROUNDING OFF THE BILL TO ZERO DECIMAL PLACES*/
			$bill = round ($bill,Constant::ROUND_RATING);
			$total_split_ac_bill = round ($total_split_ac_bill,Constant::ROUND_RATING);
			$total_window_ac_bill = round ($total_window_ac_bill,Constant::ROUND_RATING);
			
			if ($meter_phase == Constant::SINGLE_PHASE_METER && $bill < $value['single_phase_min']) $bill = $value['single_phase_min'];
			if ($meter_phase == Constant::THREE_PHASE_METER && $bill < $value['three_phase_min']) $bill = $value['three_phase_min'];
			
			$grand_total = $bill + $water_charges + $tv_charges + $meter_rent + $furniture_charges + $total_split_ac_bill + $total_window_ac_bill + $gas_charges +$misc_charges;
			$bill_type_for_db = 'commercial';					
} // END IF OF commercial TYPE
			
			
			$billing = new Billing();
			
			$billing->setStationId($station_id);
			$billing->setBuildingId($building_id);
			$billing->setOccupantId($occupant_id);
			$billing->setMeterNo($meter_no);
			$billing->setConsumerNo($consumer_no);
			$billing->setBillingDate($billing_date);
			$billing->setPreviousReading($previous);
			$billing->setPresentReading($present);
			$billing->setUnitsConsumed($consumed_units);
			$billing->setWaterCharges($water_charges);
			$billing->setTvCharges($tv_charges);
			$billing->setGasCharges($gas_charges);
			$billing->setMiscCharges($misc_charges);
			$billing->setMeterRent($meter_rent);
			$billing->setUnitsTotal($bill);
			$billing->setUnitsFree($free_unit);
			$billing->setFurnitureCharges($furniture_charges);
			$billing->setSplitCharges($total_split_ac_bill);
			$billing->setWindowCharges($total_window_ac_bill);
			$billing->setGrandTotal($grand_total);
			$billing->setPrintStatus(Constant::BOOLEAN_WORD_NO);
			$billing->setPaymentStatus(Constant::BOOLEAN_WORD_NO);
			$billing->setBillType($bill_type_for_db);
			$billing->setBillingCategory('Flat Rate');
			$billing->setStatus(Constant::RECORD_STATUS_ACTIVE);
			
			$billing->save();
			$this->getUser()->setFlash('SUCCESS_MESSAGE', Constant::RECORD_ADDED_SUCCESSFULLY);
			$this->redirect ('Billing/index');
		}
  }// END - executeMakeFlatBill
  
   public function executeElecBillPayment(sfWebRequest $request)
  {
    	$selected = $request->getParameter('selected');
		
		foreach ($selected as $i=>$select)
		{
			$bill = BillingPeer::retrieveByPk($select);
			$bill->setPaymentStatus(Constant::BILL_PAID);
			
			$bill->save();
		}
		
		$this->getUser()->setFlash('SUCCESS_MESSAGE', Constant::BILL_PAID_SUCCESS);
		$this->redirect ('Billing/index');
	
  } // END - executeElecBillPayment
  
   public function executeListPaidBill(sfWebRequest $request)
  {
    if($request->isMethod('POST'))
	{
		$billing_month = $request->getParameter('billing_month');
		$billing_year = $request->getParameter('billing_year');
		$station_id = $request->getParameter('station_id');
		$billing_date = $billing_year.'-'.$billing_month.'-'.'01';
			
		$d = new Criteria();
		$d->addAscendingOrderByColumn(BillingPeer::CONSUMER_NO);
		//$d->add(BillingPeer::BILL_TYPE, 'officer');
		$d->add(BillingPeer::BILLING_DATE, $billing_date);
		$d->add(BillingPeer::STATION_ID, $station_id);
		$d->add(BillingPeer::STATUS, Constant::RECORD_STATUS_ACTIVE);
		$d->add(BillingPeer::PAYMENT_STATUS, Constant::BILL_PAID);
		$this->bills = BillingPeer::doSelect($d);
		
		$display = BillingPeer::doSelectOne($d);
		
		$this->flag = true;
		if($display)
		{
			$this->station = $display->getStation();
			$this->bill_month = $display->getBillingDate('M-Y');
		}
	}
	
	else
	{
		$this->flag = false;
	}
  } // END - executeListPaidBill
  
  public function executeRevertElecBillPayment(sfWebRequest $request)
  {
    	
		if ($request->getParameter('encry') == 'yes')
		{
			$selected = Utility::DecryptQueryString($request->getParameter('selected'));
			$bill = BillingPeer::retrieveByPk($selected);
			$bill->setPaymentStatus(Constant::BILL_UNPAID);
			$bill->save();
			$this->getUser()->setFlash('SUCCESS_MESSAGE', Constant::BILL_UNPAID_SUCCESS);
			$this->redirect ('Billing/index');
		}

		else
		{
			$selected = $request->getParameter('selected');
			foreach ($selected as $i=>$select)
			{
				$bill = BillingPeer::retrieveByPk($select);
				$bill->setPaymentStatus(Constant::BILL_UNPAID);
				$bill->save();
			}
			$this->getUser()->setFlash('SUCCESS_MESSAGE', Constant::BILL_UNPAID_SUCCESS);
			$this->redirect ('Billing/index');
		}
  } // END - executeRevertElecBillPayment
  
  public function executeDeleteElecBill(sfWebRequest $request)
  {
		$bill = BillingPeer::retrieveByPk(Utility::DecryptQueryString($request->getParameter('id')));
		$bill->delete();
		$this->getUser()->setFlash('SUCCESS_MESSAGE', Constant::RECORD_STATUS_DELETED_SUCCESSFULLY);
		$this->redirect('Billing/index');

		
  } // END - executeDeleteElecBill
  
  
}// END - Class